<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Models\Device;
use App\Models\Project;
use App\Models\ProjectUser;
use App\Models\AdminUser;
use Hash;
use Log;
use App\Models\Task;
//20210319
class MessageController extends Controller
{
    public function message(Request $request){
        $info = $request->all();
        // $user_id = getAdminId($info['adminId']);
        // if(isset($info['type'])&&$info['type']==1){ //0报警，1任务消息
        //     $status = $info['status']==0?1:2; //1未处理，2已处理
        //     $message = Db::table('xfy_task_user')->where(['xfy_task_user.user_id'=>$info['adminId'],'xfy_task_user.status'=>$status])
        //     ->join('xfy_task','xfy_task_user.taskId','=','xfy_task.taskId')
        //     ->join('admin_users','xfy_task_user.user_id','=','admin_users.id')
        //     ->select('xfy_task.*', 'xfy_task_user.status as ustatus','admin_users.name')
        //     ->get()->toArray();
        //     // print_r($message);die;
        //     if ($message) {
        //         $res['resultCode'] = 1;
        //         $res['resultType'] = 1;
        //         $res['resultBody'] = $message;
        //     } else {
        //         $res['resultCode'] = 0;
        //         $res['resultType'] = 1;
        //         $res['resultMsg'] = '暂无任务';
        //     }
        // }else{
            $project = ProjectUser::where(['user_id'=>$info['adminId']])->pluck('projectId')->toArray();
            // $project = Db::table('xfy_project')->where(['user_id'=>getAdminId($info['adminId'])])->pluck('projectId')->toArray();
            // if ($project) {
                // $status = isset($info['status'])?$info['status']:0; //0未处理，1已处理
                $device = Device::whereIn('projectId',$project)->pluck('deviceId')->toArray();
                $where1['xfy_device_log.status'] = 0;
                $message = Db::table('xfy_device_log')->where($where1)->whereIn('xfy_device_log.deviceId',$device)
                            ->orderBy('xfy_device_log.created_at','DESC')
                            ->join('xfy_device', 'xfy_device_log.deviceId', '=', 'xfy_device.deviceId')
                            ->join('xfy_project', 'xfy_device.projectId', '=', 'xfy_project.projectId')
                            ->limit(10)->select('xfy_device_log.*','xfy_project.projectname','xfy_device.address','xfy_device.cid','xfy_device.province','xfy_device.city','xfy_device.area')->get()->toArray();
                            // print_r($message);die;
                if ($message) {
                    $res['resultCode'] = 1;
                    $res['resultType'] = 0;
                    $res['resultBody'] = $message;
                } else {
                    $res['resultCode'] = 0;
                    $res['resultType'] = 0;
                    $res['resultMsg'] = '暂无报警';
                }
            // }else{
            //     $res['resultCode'] = 0;
            //     $res['resultType'] = 0;
            //     $res['resultMsg'] = '暂无报警';
            // }
        // }
        return $res;
    }

    //完成任务
    public function CompletionTask(Request $request){
        $info = $request->all();
        $res = Db::table('xfy_task_user')->where(['taskId'=>$info['taskId'],'user_id'=>$info['adminId']])->update(['remark'=>$info['content'],'status'=>2]);
        $count = Db::table('xfy_task_user')->where(['taskId'=>$info['taskId'],'status'=>1])->count();
        if($count==0){
            Task::where(['taskId'=>$info['taskId']])->update(['status'=>2]);
        }
        if($res){
            $result['resultCode'] = 1;
            $reqult['resultMsg'] = '提交成功';
        }else{
            $result['resultCode'] = 0;
            $reqult['resultMsg'] = '提交失败';
        }
        return $result;
    }
    //处理报警
    public function solution(Request $request){
        $info = $request->all();
        $body['user_id'] = $info['adminId'];
        $body['remark'] = $info['reason'];
        $body['result'] = $info['result'];
        $body['status'] = 1;
        $res = Db::table('xfy_device_log')->where(['id'=>$info['id']])->update($body);
        if($res){
            $result['resultCode'] = 1;
            $reqult['resultMsg'] = '提交成功';
        }else{
            $result['resultCode'] = 0;
            $reqult['resultMsg'] = '提交失败';
        }
        return $result;
    }
    //任务列表
    public function mission_list(Request $request){
        $info = $request->all();
        $mission = Db::table('xfy_task_user')->where(['xfy_task_user.user_id'=>$info['adminId'],'xfy_task_user.status'=>1])
                    ->join('xfy_task', 'xfy_task.taskId', '=', 'xfy_task_user.taskId')
                    ->select('xfy_task_user.*','xfy_task.taskname','xfy_task.taskcontent','xfy_task.starttime','xfy_task.endtime')->get()->toArray();
        foreach ($mission as $key => &$value) {
            $user = Db::table('xfy_task_user')->where(['taskId'=>$value->taskId])->pluck('user_id')->toArray();
            $userlist = Db::table('admin_users')->whereIn('id',$user)->pluck('name')->toArray();
            $value->userlist = trim(implode(',',$userlist),',');
        }
        if($mission){
            $body['resultBody'] = $mission;
            $body['resultCode'] = 1;
        }else{
            $body['resultMsg'] = '暂无任务';
            $body['resultCode'] = 0;
        }
        return $body;
    }
    //任务详情
    public function mission_detail(Request $request){
        $info = $request->all();
        $task = Task::where(['taskId'=>$info['id']])->first()->toArray();
        $task['list'] = Db::table('xfy_task_user')->where(['xfy_task_user.taskId'=>$info['id']])
        ->join('admin_users','admin_users.id','=','xfy_task_user.user_id')
        ->select('xfy_task_user.*','admin_users.name')->get()->toArray();
        return $task;
    }
    //添加任务
    public function addmission(Request $request){
        $info = $request->all();
        $member = explode(',',$info['member']);
        $body['user_id'] = $info['member'];
        $body['taskname'] = $info['taskname'];
        $body['taskcontent'] = $info['taskcontent'];
        $body['admin_id'] = $info['adminId'];
        $body['starttime'] = $info['starttime'];
        $body['created_at'] = date("Y-m-d H:i:s");
        $body['endtime'] = $info['endtime'];
        $taskid = Db::table('xfy_task')->insertGetId($body);
        if($taskid){
            foreach ($member as $value){
                $member_body['taskId'] = $taskid;
                $member_body['user_id'] = $value;
                $member_body['created_at'] = date("Y-m-d H:i:s");
                DB::table('xfy_task_user')->insert($member_body);
            }
            $result['resultCode'] = 1;
            $result['resultMsg'] = '发布成功';
        }else{
            $result['resultCode'] = 0;
            $result['resultMsg'] = '发布失败';
        }
        return $request;
    }
}